package com.lagou.goods.controller;

import com.lagou.domain.Goods;
import com.lagou.goods.service.GoodsService;
import com.lagou.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Value("${server.port}")
    private String port;


    @GetMapping("getPort")
    public String getPort(){
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return port;
    }

    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public Goods getGoodsById(@PathVariable Integer id){
        return goodsService.getGoodsById(id);
    }

    /**
     * 根据id删除商品信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public String deleteGoodsById(@PathVariable Integer id){
        goodsService.deleteGoodsById(id);
        return "success";
    }

    /**
     * 更新商品信息
     * @param goods
     * @return
     */
    @PutMapping("update")
    public String updateGoods(@RequestBody Goods goods){
        goodsService.updateGoodsById(goods);
        return "success";
    }

    /**
     * 分页查询商品信息
     * @param goodsVO
     * @return
     */
    @RequestMapping("/getGoodsByPage")
    public Map<String,Object> getGoodsByPage(@RequestBody GoodsVO goodsVO){
        Map<String, Object> goodsByPage = goodsService.getGoodsByPage(goodsVO);
        return goodsByPage;
    }

    @PostMapping("/getGoodsByIds")
    public List<Goods> getGoodsByIds(@RequestBody List<Integer> ids){
        return goodsService.getGoodsByIds(ids);
    }
}
